/*
 * Copyright (c) 2016 zhangcm. All Rights Reserved.
 */
package com.deram.ctin.common.utils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author zhangcm
 * @version 1.0, 2016/12/20
 */
public class RegexpUtils {

    private RegexpUtils(){}

    /**
     * 查找匹配字符串(一次)
     * @param regex 正则字符串
     * @param content 内容
     * @return 匹配的字符串
     */
    public static String findOnce(String regex, String content){
        Matcher matcher = createMatcher(regex, content);
        return matcher.find() ? matcher.group() : null;
    }

    /**
     * 正則字符串和内容是否匹配
     * @param regex 正则字符串
     * @param content 内容
     * @return
     */
    public static Boolean match(String regex, String content){
        Matcher matcher = createMatcher(regex, content);
        return matcher.matches();
    }

    /**
     * 内容是否包换正則字符串
     * @param regex 正则字符串
     * @param content
     * @return
     */
    public static Boolean contain(String regex, String content){
        Matcher matcher = createMatcher(regex, content);
        return matcher.find();
    }

    private static Matcher createMatcher(String regex, String content){
        Pattern pattern = Pattern.compile(regex);
        return pattern.matcher(content);
    }
}
